package com.ledim.ledimview;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ledim.activity.LedimFollowUsersActivity;
import com.ledim.activity.NewPushMsgActivity;
import com.ledim.activity.UserInformationActivity;
import com.ledim.bean.LedimPicBean;
import com.ledim.bean.LedimUserBean;
import com.ledim.bean.UserSignDataBean;
import com.ledim.utils.ConstantUtil;
import com.ledim.utils.FrescoUtil;
import com.ledim.utils.LedimFunctionUtils;
import com.ledim.utils.LedimSpUtils;
import com.ledim.utils.LogUtil;
import com.letv.android.young.client.R;

/**
 * Created by qjq on 16/8/17.
 */
public class LedimBaseUserInfoView extends FrameLayout implements View.OnClickListener {

    private Context mContext;
    private LinearLayout mUserSign;
    private TextView mSignCount;
    private LinearLayout mUserNameSex;
    private TextView mUserName;
    private ImageView mUserSex;
    private SimpleDraweeView mUserPhoto;
    private TextView tv_focus_count;
    private TextView tv_fan_count;
    private LinearLayout mUserLevel;
    private LinearLayout ll_focus_fan;
    private OnClickListener mListener;
    private Intent intent;
    private ImageView mUserIdIcon;
    private TextView mUserIdContent;
    private LedimUserBean mUserData;
    private boolean mIsFromUserInfo;
    private ImageView mUserMessage;

    public LedimBaseUserInfoView(Context context) {
        super(context);
        mContext = context;
        initView();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public LedimBaseUserInfoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();

    }

    public LedimBaseUserInfoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView();
    }

    /**
     * 初始化view
     */
    private void initView() {

        View userInfoView = View.inflate(mContext, R.layout.base_userinfo_view, null);
        //签到
        mUserSign = (LinearLayout) userInfoView.findViewById(R.id.ll_user_sign);
        mUserSign.setOnClickListener(this);
        mSignCount = (TextView) userInfoView.findViewById(R.id.tv_user_sign_count);

        //用户姓名和性别
        mUserNameSex = (LinearLayout) userInfoView.findViewById(R.id.ll_username_sex);
        mUserName = (TextView) userInfoView.findViewById(R.id.tv_username);
        mUserSex = (ImageView) userInfoView.findViewById(R.id.iv_user_sex);

        mUserLevel = (LinearLayout) userInfoView.findViewById(R.id.ll_user_level);

        //用户身份等级
        mUserIdIcon = (ImageView) userInfoView.findViewById(R.id.iv_user_identity_icon);
        mUserIdContent = (TextView) userInfoView.findViewById(R.id.tv_user_identity_content);

        //关注和粉丝
        ll_focus_fan = (LinearLayout) userInfoView.findViewById(R.id.ll_focus_fan);

        //用户头像
        mUserPhoto = (SimpleDraweeView) userInfoView.findViewById(R.id.user_photo);
        mUserPhoto.setOnClickListener(this);

        //用户消息
        mUserMessage = (ImageView) userInfoView.findViewById(R.id.iv_user_message);
        mUserMessage.setOnClickListener(this);

        //消息相关设置
        showMsgInfo();

        //关注和粉丝layout
        LinearLayout ll_fan_layout = (LinearLayout) userInfoView.findViewById(R.id.ll_fan_layout);
        LinearLayout ll_focus_layout =
                (LinearLayout) userInfoView.findViewById(R.id.ll_focus_layout);
        ll_fan_layout.setOnClickListener(this);
        ll_focus_layout.setOnClickListener(this);

        //关注
        tv_focus_count = (TextView) userInfoView.findViewById(R.id.tv_focus_count);
        //粉丝
        tv_fan_count = (TextView) userInfoView.findViewById(R.id.tv_fan_count);

        addView(userInfoView);

    }

    /**
     * 设置粉丝和关注的数量
     *
     * @param followCount
     * @param fanCount
     */
    public void setFollowAndFansCounts(int followCount, int fanCount) {
        if (tv_focus_count != null && tv_fan_count != null) {
            tv_focus_count.setText(String.valueOf(followCount));
            tv_fan_count.setText(String.valueOf(fanCount));
        }
    }

    /**
     * 设置布局显示(用户登录)
     */
    public void setLayoutVisible() {
        //用户登录
        mUserNameSex.setVisibility(View.VISIBLE);
        mUserLevel.setVisibility(View.VISIBLE);
        ll_focus_fan.setVisibility(View.VISIBLE);

    }

    /**
     * 设置签到布局隐藏
     */
    public void setSignInvisible() {
        mUserSign.setVisibility(View.GONE);
        mSignCount.setVisibility(View.GONE);
    }

    /**
     * 设置布局隐藏(用户未登录)
     */
    public void setLayoutInvisible() {
        mUserNameSex.setVisibility(View.GONE);
        mUserLevel.setVisibility(View.GONE);
        //图片弄成默认的
        mUserPhoto.setImageResource(R.drawable.profile_user_default_icon);
        ll_focus_fan.setVisibility(View.GONE);
    }

    public void initSetting(OnClickListener listener, boolean isFromUserInfo) {
        mListener = listener;
        mIsFromUserInfo = isFromUserInfo;
        initView();
    }

    /**
     * 设置用户信息
     */
    public void setUserInfo(LedimUserBean userBean) {
        mUserData = userBean;
        //设置用户信息
        mUserName.setText(userBean.nickname);
        //性别判断
        if (userBean.gender.equals("1")) {
            mUserSex.setImageResource(R.drawable.profile_man);
        } else if (userBean.gender.equals("2")) {
            mUserSex.setImageResource(R.drawable.profile_woman);
        } else if (userBean.gender.equals("0")) {
            mUserSex.setImageResource(R.drawable.profile_agender);
        }

        LedimFunctionUtils.getUserTypeIcon(userBean.status);
        //判断身份
        if (userBean.status.equals("0")) {
            //普通用户
            mUserIdIcon.setImageResource(R.drawable.profile_user_level_normal);
            mUserIdContent.setText(R.string.user_id1);
        } else if (userBean.status.equals("1")) {
            //大神
            mUserIdIcon.setImageResource(R.drawable.user_level_biggod);
            mUserIdContent.setText(R.string.user_id2);

        } else if (userBean.status.equals("2")) {
            //官方
            mUserIdIcon.setImageResource(R.drawable.user_level_authority);
            mUserIdContent.setText(R.string.user_id3);
        }
        //签到
        if (userBean.is_signed) {
            //已签到
            String signDes = "已签到" + userBean.sign_count + "天";
            Spannable spannableString = new SpannableString(signDes);
            spannableString.setSpan(new AbsoluteSizeSpan(60), 3,
                    spannableString.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            mSignCount.setText(spannableString);
            mUserSign.setVisibility(View.GONE);
            mSignCount.setVisibility(View.VISIBLE);
        } else {
            //未签到
            mUserSign.setVisibility(View.VISIBLE);
            mSignCount.setVisibility(View.GONE);
        }
        //头像
        LedimPicBean avatar = userBean.avatar;
        if (avatar != null && !TextUtils.isEmpty(avatar.thumb)) {
            FrescoUtil.loadUrl(avatar.thumb, mUserPhoto);
        }
        //粉丝和关注数量
        tv_fan_count.setText(String.valueOf(userBean.fan));
        tv_focus_count.setText(String.valueOf(userBean.focus));

    }

    /**
     * 显示消息信息,不需要在网络请求之后进行设置，因为本地存储的
     */
    public void showMsgInfo() {
        if (mIsFromUserInfo) {
            mUserMessage.setVisibility(View.GONE);
        } else {
            mUserMessage.setVisibility(View.VISIBLE);
            LogUtil.e("ccx pusmsgcount===" + LedimSpUtils.getPushMgsCount());
            if (LedimSpUtils.getPushMgsCount() > 0) {

                mUserMessage.setImageResource(R.drawable.have_message);
            } else {
                mUserMessage.setImageResource(R.drawable.no_message);
            }
        }
    }

    /**
     * 设置签到用户信息
     */
    public void setSignInfo(UserSignDataBean userSignBean) {
        String signDes = "已签到" + userSignBean.sign_count + "天";
        Spannable spannableString = new SpannableString(signDes);
        spannableString.setSpan(new AbsoluteSizeSpan(60), 3,
                spannableString.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mSignCount.setText(spannableString);
        mUserSign.setVisibility(View.GONE);
        mSignCount.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //签到页面
            case R.id.ll_user_sign:
                if (mListener != null) {
                    mListener.onClick(v);
                }
                break;
            //用户头像
            case R.id.user_photo:
                if (!mIsFromUserInfo) {
                    if (mUserData != null) {
                        intent = new Intent(mContext, UserInformationActivity.class);
                        intent.putExtra(UserInformationActivity.USER_INFO, mUserData);
                        mContext.startActivity(intent);
                        ((Activity) mContext).overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
                    }
                }
                break;

            case R.id.ll_focus_layout:
                //跳转到关注
                if (mUserData != null && Integer.valueOf(tv_focus_count.getText().toString()) > 0) {
                    intent = new Intent(mContext, LedimFollowUsersActivity.class);
                    if (LedimSpUtils.isMe(mUserData.userId)) {
                        intent.putExtra(ConstantUtil.TITLE, "我的关注");
                    } else {
                        intent.putExtra(ConstantUtil.TITLE, mUserData.nickname + "的关注");
                    }
                    intent.putExtra(ConstantUtil.RELATION, "1");
                    intent.putExtra(ConstantUtil.ID, mUserData.userId);
                    mContext.startActivity(intent);
                    ((Activity) mContext).overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
                }
                break;

            case R.id.ll_fan_layout:
                //跳转到粉丝页面
                if (mUserData != null && Integer.valueOf(tv_fan_count.getText().toString()) > 0) {
                    intent = new Intent(mContext, LedimFollowUsersActivity.class);
                    if (LedimSpUtils.isMe(mUserData.userId)) {
                        intent.putExtra(ConstantUtil.TITLE, "我的粉丝");
                    } else {
                        intent.putExtra(ConstantUtil.TITLE, mUserData.nickname + "的粉丝");
                    }
                    intent.putExtra(ConstantUtil.RELATION, "2");
                    intent.putExtra(ConstantUtil.ID, mUserData.userId);
                    mContext.startActivity(intent);
                    ((Activity) mContext).overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
                }
                break;
            case R.id.iv_user_message:
                intent = new Intent(mContext, NewPushMsgActivity.class);
                mContext.startActivity(intent);
                ((Activity) mContext).overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
                break;
            
            default:

                break;

        }

    }
}
